﻿using System;
using System.Collections.Generic;
using System.Text;
using FunctionalCLR.Core;

namespace FunctionalCLR.Core.Function.List {

    /// <summary>
    /// lst = union([1,2], [2,3], [,5,7]);
    /// echo(lst);
    /// 
    /// the expected result is [1,2,3,5,7]
    /// </summary>
    public class ListRankNode : FuncNode {

        public ListRankNode()
            : base() {
            this.Value = "rank";
        }

        public override object Evaluate() {
            foreach (Node child in Children) {
                ((ListNode)child).Evaluate();
            }

            List<string> values = new List<string>();
            ListNode result = new ListNode();

            for (int i = 0; i < Children.Count; i++) {
                ListNode c = (ListNode)Children[i];
                foreach (Node u in c.Children) {
                    if (!values.Contains(u.Value)) {
                        result.Children.Add(u);
                        values.Add(u.Value);
                    }
                }
            }

            return result;
        }
    }
}
